package ai.people.platform.system.controller;

import ai.people.netmon.framework.domain.system.entity.NmSystemRole;
import ai.people.netmon.framework.domain.system.request.AddSystemRoleRequest;
import ai.people.netmon.framework.domain.system.request.SysRoleRequest;
import ai.people.netmon.framework.domain.system.request.UpdateSystemRoleRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.system.service.SystemRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色接口
 *
 * @author yuanqinglong
 * @date 2022/3/7 10:08
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
public class SystemRoleController {


    /**
     * 系统角色服务impl
     */
    private final SystemRoleService systemRoleServiceImpl;


    /**
     * 角色列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link NmSystemRole}>>
     */
    @PostMapping("/list")
    public Result<PageResponse<NmSystemRole>> listRoles(@RequestBody SysRoleRequest request) {
        PageResponse<NmSystemRole> result = systemRoleServiceImpl.listRoles(request);
        return ResultUtil.success(result);
    }

    /**
     * 通过id获取角色
     *
     * @param id id
     * @return {@link Result}<{@link NmSystemRole}>
     */
    @GetMapping("/{id}")
    public Result<NmSystemRole> getRolesById(@PathVariable("id") Long id) {
        NmSystemRole result = systemRoleServiceImpl.getRolesById(id);
        return ResultUtil.success(result);
    }

    /**
     * 查询用户角色
     *
     * @param userId 用户id
     * @return {@link Result}<{@link List}<{@link NmSystemRole}>>
     */
    @GetMapping("/userRoles")
    public Result<List<NmSystemRole>> userRoles(@RequestParam("userId") Long userId) {
        List<NmSystemRole> nmSystemRoles = systemRoleServiceImpl.userRoles(userId);
        return ResultUtil.success(nmSystemRoles);
    }

    /**
     * 保存角色
     *
     * @param roleRequest 角色
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping()
    public Result<Boolean> saveRoles(@Validated @RequestBody AddSystemRoleRequest roleRequest) {
        systemRoleServiceImpl.saveRoles(roleRequest);
        return ResultUtil.success();
    }

    /**
     * 更新角色
     *
     * @param updateRoleRequest 角色参数
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping
    public Result<Boolean> updateRoles(@Validated @RequestBody UpdateSystemRoleRequest updateRoleRequest) {
        systemRoleServiceImpl.updateRoles(updateRoleRequest);
        return ResultUtil.success();
    }


    /**
     * 更新启用状态
     *
     * @param roleId  角色id
     * @param enabled 启用状态 true ：启用 、false ：禁用
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping("/{roleId}/{enabled}")
    public Result<Boolean> updateEnabled(@PathVariable("roleId") Long roleId, @PathVariable("enabled") Boolean enabled) {
        systemRoleServiceImpl.updateEnabled(roleId, enabled);
        return ResultUtil.success();
    }

    /**
     * 删除角色
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteRoles(@PathVariable("id") Long id) {
        systemRoleServiceImpl.deleteRoles(id);
        return ResultUtil.success();
    }
}
